package com.le.tester.javaThink.fifth.chapter14_StreamingProgramming;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * 描述：stream test
 */

class Student {
    private String name;
    private Long age;

    public Student(String name, Long age) {
        this.name = name;
        this.age = age;
    }

    public Student() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getAge() {
        return age;
    }

    public void setAge(Long age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class StreamTest {

    private static final Logger LOG = LoggerFactory.getLogger(StreamTest.class);

    public void createStream() {
        List<String> values = new ArrayList<>();
        values.add("a");
        values.add("b");
        values.add("c");
        Stream<String> stream = values.stream();
        Stream<String> stringStream = values.parallelStream();
        System.out.println(values);
        LOG.info("result:{}", stream.toArray());
        LOG.info("second result:{}", stringStream.toArray());
    }

    public void chooseStream() {
        Stream<Integer> integerStream = Stream.of(6, 4, 6, 7, 3, 9, 8, 10, 12, 14, 14);
        Stream<Integer> result = integerStream.filter(s -> s > 5).distinct().skip(2).limit(2);
        //使用foreach输出值
        result.forEach(System.out::print);
        //LOG.info("result:{}", result);
    }

    public void mapStream() {
        List<String> strings = Arrays.asList("a,b,c", "1,2,3");
        //只要是有逗号，就全部替换掉了
        Stream<String> stringStream = strings.stream().map(s -> s.replaceAll(",", ""));
        stringStream.forEach(System.out::println);

        Stream<String> stream = strings.stream().flatMap(o -> {
            String[] split = o.split(",");
            return Arrays.stream(split);
        });
        stream.forEach(System.out::print);
    }

    public void peekStream() {
        List<String> strings = Arrays.asList("aa", "cc", "bb");
        //装成stream处理排序问题
        strings.stream().sorted().forEach(System.out::print);
        Student aa = new Student("aa", 10L);
        Student bb = new Student("bb", 20L);
        List<Student> students = Arrays.asList(aa, bb);
        //这个可以使用builder的模式
        //空值的时候可以使用这个方式
        students.stream().
                peek(o -> o.setAge(100L)).forEach(System.out::print);
    }


    public static void main(String[] args) {
        StreamTest streamTest = new StreamTest();
        //streamTest.createStream();
        //streamTest.chooseStream();
        //streamTest.mapStream();
        //streamTest.peekStream();
        String stream_id = "1400382402_25_share_share-user_39_25_problem_4999_recordSource_1_main";
        if (stream_id.contains("recordSource")) {
            String[] recordSourceS = stream_id.split("_recordSource_");
            System.out.println(Arrays.toString(recordSourceS));
            String[] recordSourceAndMain = recordSourceS[1].split("_");
            System.out.println(Arrays.toString(recordSourceAndMain));
            Long result = Long.parseLong(recordSourceAndMain[0]);
            System.out.println(result);
        }


    }
}
